package myadmin.jtthink.com.MyServices;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import myadmin.jtthink.com.Mapper.MatcherAccessMapper;
import myadmin.jtthink.com.Models.AuthResponse;
import myadmin.jtthink.com.Models.ListOption;
import myadmin.jtthink.com.Models.MatcherAccessModel;

import myadmin.jtthink.com.MyAnnotations.MethodName;
import myadmin.jtthink.com.MyAnnotations.POST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class MatcherService {
    @Autowired
    MatcherAccessMapper matcherAccessMapper;
    @POST
    @MethodName("list") //matcher列表
    public AuthResponse matcherList(@RequestBody ListOption listOption){
        QueryWrapper<MatcherAccessModel> queryWrapper=new QueryWrapper<>();
        if(!listOption.getKey().trim().equals("")){
            queryWrapper.like("path",listOption.getKey().trim());
        }
        Page<MatcherAccessModel> p=new Page<>(listOption.getPageNo(),listOption.getPageSize());
        IPage<MatcherAccessModel> result= this.matcherAccessMapper.selectPage(p,queryWrapper);
        return AuthResponse.build(result.getRecords(),
                result.getCurrent(),result.getSize(),result.getTotal());

    }
    @POST
    @MethodName("save") //保存matcher
    public int matcherSave(@RequestBody MatcherAccessModel matcherAccessModel){
        if(matcherAccessModel.getId()>0){
            return this.matcherAccessMapper.updateById(matcherAccessModel);
        }
        return this.matcherAccessMapper.insert(matcherAccessModel);
    }
    @POST //获取matcher详细
    @MethodName("detail") //获取matcher详细
    public MatcherAccessModel getMatcher(@RequestBody MatcherAccessModel matcherAccessModel){
        return this.matcherAccessMapper.selectById(matcherAccessModel.getId());
    }

}
